package com.knife.blog.web.action;

import com.knife.blog.domain.BlogResource;
import com.knife.blog.logic.BlogResourceService;
import com.knife.blog.logic.impl.BlogResourceServiceImpl;
import com.knife.util.CommUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;

public class BlogResourceAction extends BaseCrudAction {

	private BlogResourceService resourceService=BlogResourceServiceImpl.getInstance();
	
	public IPageList doQuery(WebForm form, int currentPage, int pageSize) {
		return resourceService.queryBlogResource(this.getCurrentUser(form).getUserName(), currentPage, pageSize);
	}
	
    @Override
	public Page doAdd(WebForm form, Module module, IActiveUser user) {
		boolean ret=resourceService.addBlogResource((BlogResource)form2Obj(form));
		com.knife.blog.logic.HTMLGenerater.saveToHTML(blog);
		//com.knife.blog.logic.HTMLGenerater.savePhotoDirIndexToHTMLByBlog(blog);//用户相册首页		
		if(ret)
		{
			form.addResult("msg","添加成功!");
			return super.doQuery(form,module,user);	
		}
		else
		{
			form.addResult("msg","添加失败!");
			return module.findPage("edit");
		}
	}

    @Override
	public Page doDel(WebForm form, Module module, IActiveUser user) {		
		boolean ret=resourceService.delBlogResource((String)form.get("cid"));
		com.knife.blog.logic.HTMLGenerater.saveToHTML(blog);
		//com.knife.blog.logic.HTMLGenerater.savePhotoDirIndexToHTMLByBlog(blog);//用户相册首页
		form.addResult("msg",ret?"删除成功!":"删除失败!");		
		return super.doQuery(form,module,user);	
	}

    @Override
	public Page doUpdate(WebForm form, Module module, IActiveUser user) {
		boolean ret=resourceService.updateBlogResource((BlogResource)form2Obj(form));
		com.knife.blog.logic.HTMLGenerater.saveToHTML(blog);
		//com.knife.blog.logic.HTMLGenerater.savePhotoDirIndexToHTMLByBlog(blog);//用户相册首页
		form.addResult("msg",ret?"修改成功!":"修改失败!");	
		return super.doQuery(form,module,user);	
	}

    public Page doManageList(WebForm form, Module module) {
        String username = CommUtil.null2String(this.getCurrentUser(form).getUserName());
        if (!com.knife.blog.logic.BlogAdminConfig.getInstance().isAdmin(username)) {
            //System.out.println(this.getCurrentUser(form).getUserName());
            return new Page("popedomError", "/manage/norights.htm", "page");
        }
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize < 1) {
            pageSize = 15;
        }
        IPageList pageList = resourceService.queryBlogResource(username,currentPage, pageSize);
        CommUtil.saveIPageList2WebForm(pageList, form);
        return module.findPage("manageList");
    }

	public Object form2Obj(WebForm form) {
		String cid =CommUtil.null2String(form.get("cid"));
		BlogResource obj=resourceService.getBlogResource(cid);
		if(obj==null)obj=new BlogResource();
		form.toPo(obj);
		return obj;
	}
}